package com.example.mykotilin.paging2.repository.inMemory.byPage

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.example.mykotilin.paging2.RedditApi
import com.example.mykotilin.paging2.vo.RedditPost
import retrofit2.HttpException
import java.io.IOException

class PageKeyedSubredditPagingSource(
    private val redditApi:RedditApi,
    private val subredditName:String
):PagingSource<String,RedditPost>() {
    override fun getRefreshKey(state: PagingState<String, RedditPost>): String? {
        return state.anchorPosition?.let {
            anchorPosition->
            state.closestPageToPosition(anchorPosition)?.prevKey
        }
    }

    override suspend fun load(params: LoadParams<String>): LoadResult<String, RedditPost> {
        return try {
            val data = redditApi.getTop(
                subreddit = subredditName,
                after = if(params is LoadParams.Append) params.key else null,
                before = if(params is LoadParams.Prepend) params.key else null,
                limit = params.loadSize
            ).data
            LoadResult.Page(
                data = data.children.map { it.data },
                prevKey = data.before,
                nextKey = data.after
            )
        }catch (e:IOException){
            LoadResult.Error(e)
        }catch (e:HttpException){
            LoadResult.Error(e)
        }
    }
}